﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MultiDAQ_Analysis.Util
{
    public static class CRC32Custom
    {
        private const uint Poly = 0x1CDF4421; // 未反射多项式
        public static uint Compute(byte[] data)
        {
            uint crc = 0xFFFFFFFF;
            foreach (byte b in data)
            {
                crc ^= (uint)b << 24; // 高位对齐
                for (int i = 0; i < 8; i++)
                {
                    if ((crc & 0x80000000) != 0)
                        crc = (crc << 1) ^ Poly;
                    else
                        crc <<= 1;
                }
            }
            return crc; // ❗不做反码
        }

        public static byte ComputeCrcOutByte(byte[] data)
        {
            uint crc = Compute(data);
            return (byte)(~((crc >> 24) & 0xFF)); // 模拟 Verilog 的 CRC_out 输出（反码高字节）
        }
    }
}

